checkLogin(true);
$(function () {
    var pageCount;
    $.post("../php/settings/getNavMenusAll.php", function (res) {
        if (res.code == 100) {
            pageCount = res.dataCount;
            var html = $("#mt").tmpl(res.data);
            $("#dataset").empty().append(html);
        }
    }, "json");

    $("#dataset").on("click", ".del", function () {
        var nav_id = $(this).attr("data-id");
        layer.alert("您是否要删除该导航链接", {icon: 3}, function (index) {
            layer.close(index);
            $.post("../php/settings/deleteNavMenu.php", {id: nav_id}, function (res) {
                if (res.code == 100) {
                    layer.alert(res.msg, {icon: 1, title: "删除导航链接操作"}, function (index) {
                        layer.close(index);
                        location.reload();
                    })
                }
                else {
                    layer.alert(res.msg, {icon: 2, title: "删除导航链接操作"}, function (index) {
                        layer.close(index);
                        location.reload();
                    })
                }
            }, "json");
        });
    });

    $("#choose-icon").on("click", function () {
        $(".icon-list").toggle();
    });

    $(".icon-list").on("click", "span", function () {
        var cls = $(this).attr("class");
        $("#choose-icon>span").attr("class", cls);
        $("#icon").val(cls);
    })

    $("#btn-sure").on("click", function () {
        var data = $("#nav-data").serialize();
        data = data.replace("+", " ");
        $.post("../php/settings/addNavData.php", data, function (res) {
            if (res.code == 100) {
                layer.alert(res.msg, {icon: 1, title: "添加导航链接操作"}, function (index) {
                    layer.close(index);
                    location.reload();
                })
            }
            else {
                layer.alert(res.msg, {icon: 2, title: "添加导航链接操作"}, function (index) {
                    layer.close(index);
                    location.reload();
                })
            }
        }, "json");
    })

    $("thead input[type=checkbox]").on("click", function () {
        var res = $(this).prop("checked");
        $("#dataset input[type=checkbox]").prop("checked", res);
        if (res) {
            $("#del-mul").show();
        }
        else {
            $("#del-mul").hide();
        }
    })

    $("#dataset").on("click", "input[type=checkbox]", function () {
        var count = $("#dataset input[type=checkbox]:checked").size();
        $("thead input[type=checkbox]").prop("checked", count == pageCount);
        if (count >= 2) {
            $("#del-mul").show();
        }
        else {
            $("#del-mul").hide();
        }
    })

    $("#del-mul").on("click", function () {
        layer.confirm("您是否要删除所选取的数据", {icon: 3}, function (index) {
            layer.close(index);
            var ids = [];
            var cks = $("#dataset input[type=checkbox]:checked");
            cks.each(function (index, element) {
                var id = $(element).parent().parent().attr("nav_id");
                ids.push(id);
            })
            console.log(ids);
            $.post("../php/settings/deleteNavMenusMultiple.php", {ids: ids}, function (res) {
                if (res.code == 100) {
                    layer.alert(res.msg, {icon: 1, title: "您正在进行批量删除操作"}, function () {
                        location.reload();
                    })
                }
                else {
                    layer.alert(res.msg, {icon: 2, title: "您正在进行批量删除操作"}, function () {
                        location.reload();
                    })
                }
            },"json");
        })
    })
});